#!/usr/bin/env python
import Tkinter
import Image, ImageTk, ImageDraw

class ScreenImage(Image.Image):
    def __init__(self, widget=None, size=(400,300), color=(0,0,0)):
        width, height = size
        #Inheriting from PIL's image class is not very clean.
        #we have to re-issue the statements found in its "_new"  constructor:
        Image.Image.__init__(self)
        self.im = Image.core.fill("RGB", size, color)
        self.mode = "RGB"
        self.size = size
        self.draw = ImageDraw.Draw(self)
        self.photoimage = ImageTk.PhotoImage("RGB", (width, height))
        self.canvas = Tkinter.Canvas(widget, width=width, height=height)
        self.canvas.create_image(width // 2, height // 2, image=self.photoimage)
        self.canvas.pack()
        self.update()

    def update(self):
        """Updates memory buffer to Tkinter screen"""
        self.photoimage.paste(self)

# if __name__ == "__main__":
#     tk = Tkinter.Tk()
#     i = ScreenImage(tk)
# 
#     for x in range(640):
#         y = 240 + 120 * math.sin((x - 320) / 320.0 * 4 * math.pi)
#         i.putpixel((x, y) , (0,0,0))
#     i.update()
#     tk.mainloop()

# class Canvas(Tkinter.Canvas):
#     def __init__(self):
#         master = self._master = Tkinter.Tk()
#         Tkinter.Canvas.__init__(self, master, width=500, height=500)
# 
# if __name__ == "__main__":
#     canvas = Canvas()
#     canvas.mainloop()
